package com.airforce.controller;

import com.airforce.mappers.sys_userMapper;
import com.airforce.model.sys_user;
import com.airforce.service.LoginAuthentication;
import com.airforce.utils.MD5BASE64Util;
import com.airforce.utils.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping(value = "system")
public class Login {


    @Autowired
  private LoginAuthentication loginAuthentication;
    @Value("${salt}")
    private String salt;

    @Autowired
    sys_userMapper  sys_userMapper;




    @RequestMapping(value = "/login",method = RequestMethod.GET)

    public Object login(@RequestParam String account, @RequestParam String password) {

       sys_user user=new sys_user();
       user.setUsername(account);
       user.setPassword(password);
        if(user!=null){
         Object res= loginAuthentication.loginAuthentication(user);
            return res;
        }

        return ResultCode.Fail_LOGIN.result();
    }


    @GetMapping (value = "changePwd")
    public Object changePwd(@RequestParam String pwd,@RequestParam String newpwd,@RequestParam String token){

        String[] str=MD5BASE64Util.base64decode(token).split(",");
        String username=str[0];
            if(sys_userMapper.selectbyname(username)!=null){
                if(sys_userMapper.selectbypassword(MD5BASE64Util.md5base64encode(pwd+username+salt))!=null) {
                    sys_user user = new sys_user();
                    user.setId(sys_userMapper.selectbyname(username).getId());
                    user.setPassword(MD5BASE64Util.md5base64encode(newpwd+username+salt));
                    sys_userMapper.updateByPrimaryKeySelective(user);
                    System.out.println("——————————————-系统管理员密码修改成功——————————");
                    return ResultCode.SUCCESS.result();
                }else {
                    return ResultCode.FAIL_PASSWORD.result();
                }
            }else {

                return ResultCode.NO_USER.result();
            }


    }



}
